import Compute from './../lib/compute';
import toNumber from '../utils/tools';
// 使用类继承扩展
export default class Calculator extends Compute {
// 使用装饰器扩展
// @compute
// export default class Calculator {
    constructor(el) {
        // 使用类继承扩展
        super();
        // 模块名称
        this.name = "Calculator";
        this.result = el.querySelector('.result');
        this.inputs = el.querySelectorAll('input');
        this.btnGroup = el.querySelector('.btn-group');
    }
    init() {
        this.bindEvent();
    }
    bindEvent() {
        this.btnGroup.addEventListener('click', this.btnClick.bind(this), false);
    }
    btnClick(ev) {
        ev = ev || window.event;
        const tar = ev.target || ev.srcElement;
        const tagName = tar.tagName.toLowerCase();
        if(tagName==='button'){
            const fval = toNumber(this.inputs[0].value) || 0;
            const sval = toNumber(this.inputs[1].value) || 0;
            const method = tar.getAttribute('data-method');
            this.setResult(method, fval, sval);
        }
    }
    setResult(method,fval,sval){
        this.result.innerText = this[method](fval, sval);
    }
}